<template>
    <Card style="height:800px">
        <Form ref="form" :model="form" :rules="rules" :label-width="80">
            <FormItem label="角色代码" prop="roleCode">
                <Input v-model="form.roleCode" placeholder="" :disabled="type==1">
                </Input>
            </FormItem>
            <FormItem label="角色名称" prop="roleName">
                <Input v-model="form.roleName" placeholder="">
                </Input>
            </FormItem>
            <FormItem label="角色优先级" prop="roleLevel">
                <Input v-model="form.roleLevel" placeholder="">
                </Input>
            </FormItem>
            <FormItem label="状态" prop="status">
                <Select v-model="form.status">
                    <Option v-for="item in selectStatus" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
            </FormItem>
            <FormItem label="描述" prop="description">
                <Input v-model="form.description" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="">
                </Input>
            </FormItem>
            <FormItem v-if="type==2 || type==1">
                <Button type="primary" @click="handleSubmit">确定</Button>
                <Button type="primary" style="margin-left: 8px" @click="handleGoBack">返回</Button>
            </FormItem>
            <FormItem v-if="type==0">
                <Button type="primary" @click="handleGo(1)">编辑</Button>
                <Button type="primary" style="margin-left: 8px" @click="handleGoBack">返回</Button>
            </FormItem>
        </Form>
    </Card>
</template>

<script>
export default {
  data () {
    return {
      baseUrl: this.$appContext.marvels_core_system,
      $ajax: null,
      selectStatus: [
        {
          value: 1,
          label: '启用'
        },
        {
          value: 0,
          label: '禁用'
        }
      ],
      form: {
        roleId: null,
        roleCode: null,
        roleName: null,
        roleLevel: null,
        status: 1,
        description: null,
        createdDate: null,
        updatedDate: null,
        createdBy: null,
        updatedBy: null
      },
      rules: {
        roleCode: [
          { required: true, message: '角色代码不能为空', trigger: 'blur' }
        ],
        roleName: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        status: [
          { type: 'number', required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      },
      copyRole: {}
    }
  },
  methods: {
    handleSubmit () {
      var vm = this
      this.$refs.form.validate(valid => {
        if (valid) {
          var config = { title: '提示' }
          if (this.type === 1) {
            config.content = '是否确认修改?'
            config.onOk = vm.updateRole
          } else if (this.type === 2) {
            config.content = '是否确认新建?'
            config.onOk = vm.addRole
          }
          vm.$Modal.confirm(config)
        }
      })
    },
    addRole: function () {
      var params = this.form
      this.$ajax.post(this.baseUrl + 'system/pmsn/service/addRole', params).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        this.handleGoBack()
      }).catch(er => {

      })
    },
    handleGoBack: function () {
      this.$router.back()
    },
    updateRole: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          var params = this.form
          this.$ajax.post(this.baseUrl + 'system/pmsn/service/updateRole', params).then(re => {
            var data = re.data
            var code = data.code
            var message = data.message
            if (code !== 'success') {
              this.$Message.warning(message)
              return
            }
            this.handleGoBack()
          }).catch(er => {

          })
        }
      })
    },
    initpage: function () {
      if (this.type === 1) { // 编辑
        var query = this.$route.query
        this.form.roleId = query.index
        this.renderForm()
      } else {
        this.copyRole = JSON.parse(JSON.stringify(this.roleObj))
      }
    },
    renderForm: function () {
      var params = { roleId: this.form.roleId }
      this.$ajax.post(this.baseUrl + 'system/pmsn/service/roleDetail', params).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        this.form = data.data
        this.copyRole = JSON.parse(JSON.stringify(this.form))
      }).catch(er => {

      })
    }
  },
  computed: {
    type () { // 0 查看 1编辑 2新增
      return parseInt(this.$route.query.type)
    }
  },
  created () {
    this.$ajax = this.$util.ajax()
    this.initpage()
  }
}
</script>
